/*     */ package com.liy.cloud.auth.client.aop;
/*     */ 
/*     */ import com.liy.cloud.auth.client.jwt.JwtTokenService;
/*     */ import com.das.framework.common.annotation.ActionPermission;
/*     */ import com.das.framework.common.annotation.IgnoreUserToken;
/*     */ import com.das.framework.common.annotation.MethodPermission;
/*     */ import com.das.framework.common.extvo.LoginInfo;
/*     */ import com.das.framework.common.utils.ContextHandler;
/*     */ import com.das.framework.common.utils.Tools;
/*     */ import com.das.framework.common.web.exception.InvalidRequestException;
/*     */ import com.das.framework.common.web.exception.NoAuthorizationException;
/*     */ import com.das.framework.common.web.utils.AopTargetUtils;
/*     */ import com.das.framework.common.web.utils.WebTools;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */
import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.beans.factory.annotation.Value;
/*     */ import org.springframework.stereotype.Component;
/*     */ import org.springframework.web.method.HandlerMethod;
/*     */ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Component
/*     */ public class AuthorizationInterceptorService
/*     */   extends HandlerInterceptorAdapter
/*     */ {
/*  36 */   private static Logger log = LoggerFactory.getLogger(AuthorizationInterceptorService.class);
/*     */ 
/*     */   
/*     */   @Value("${dascloud.authrization.enabled: true}")
/*     */   private Boolean authrizationEnabled;
/*     */ 
/*     */   
/*     */   @Value("${dascloud.authrization.token-header:JwtToken}")
/*     */   private String tokenHeader;
/*     */ 
/*     */   
/*     */   @Value("${dascloud.authrization.auth-key:AuthKey}")
/*     */   private String authKeyHeader;
/*     */   
/*     */   @Autowired
/*     */   private JwtTokenService jwtTokenService;
/*     */ 
/*     */   
/*     */   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*  55 */     log.info("new request: " + request.getRequestURL());
/*     */     
/*  57 */     if (!(handler instanceof HandlerMethod)) {
/*  58 */       throw new InvalidRequestException("无效的请求 [ " + request.getRequestURL().toString() + " ] ");
/*     */     }
/*     */ 
/*     */     
/*  62 */     if (!this.authrizationEnabled.booleanValue()) {
/*  63 */       return super.preHandle(request, response, handler);
/*     */     }
/*     */     
/*  66 */     HandlerMethod handlerMethod = (HandlerMethod)handler;
/*     */ 
/*     */     
/*  69 */     IgnoreUserToken annotation = (IgnoreUserToken)handlerMethod.getBeanType().getAnnotation(IgnoreUserToken.class);
/*  70 */     if (annotation == null) {
/*  71 */       annotation = (IgnoreUserToken)handlerMethod.getMethodAnnotation(IgnoreUserToken.class);
/*     */     }
/*  73 */     if (annotation != null) {
/*  74 */       return super.preHandle(request, response, handler);
/*     */     }
/*     */ 
/*     */ 
/*     */ 
/*     */     
/*  80 */     String authKey = WebTools.getRequesetParameterOrHeader(request, this.authKeyHeader);
/*     */ 
/*     */     
/*  83 */     String jwtToken = WebTools.getRequesetParameterOrHeader(request, this.tokenHeader);
/*     */ 
/*     */     
/*  86 */     if (Tools.StringIsNullOrSpace(jwtToken)) {
/*  87 */       throw new NoAuthorizationException("未认证的请求.");
/*     */     }
/*     */     
/*  90 */     LoginInfo loginInfo = this.jwtTokenService.getInfoFromToken(jwtToken);
/*  91 */     ContextHandler.setLoginUser(loginInfo);
/*  92 */     ContextHandler.setJwtToken(jwtToken);
/*  93 */     ContextHandler.setAuthKey(authKey);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 102 */     return super.preHandle(request, response, handler);
/*     */   }
/*     */ 
/*     */   
/*     */   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
/* 107 */     ContextHandler.remove();
/* 108 */     super.afterCompletion(request, response, handler, ex);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private StringBuffer getPermissionCodes(Object handler) throws Exception {
/* 158 */     StringBuffer permissionBuffer = null;
/* 159 */     HandlerMethod method = (HandlerMethod)handler;
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 164 */     Class<?> clazz = AopTargetUtils.getTarget(method.getBean()).getClass();
/* 165 */     ActionPermission actionPermission = (ActionPermission)clazz.getAnnotation(ActionPermission.class);
/* 166 */     if (actionPermission != null && !Tools.StringIsNullOrSpace(actionPermission.code())) {
/*     */       
/* 168 */       MethodPermission methodPermission = (MethodPermission)method.getMethodAnnotation(MethodPermission.class);
/* 169 */       if (methodPermission != null && methodPermission.codes() != null && methodPermission.codes().length > 0) {
/*     */         
/* 171 */         permissionBuffer = new StringBuffer();
/* 172 */         for (String code : methodPermission.codes()) {
/* 173 */           permissionBuffer.append(actionPermission.code()).append("&").append(code).append(";");
/*     */         }
/*     */       } 
/*     */     } 
/* 177 */     return permissionBuffer;
/*     */   }
/*     */ }


/* Location:              C:\Users\admin\Desktop\Jar\das-cloud-osc\das-cloud-osc-auth-client-1.0.0-SNAPSHOT.jar!\com\das\cloud\auth\client\aop\AuthorizationInterceptorService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */